package com.huhu.controller;

import com.huhu.controller.base.BaseController;
import com.huhu.dto.Result;
import com.huhu.entity.User;
import com.huhu.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @TODO Created by xuyi on 2016/9/6.
 */
@Controller
@RequestMapping("/userController")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private CredentialsMatcher credentialsMatcher;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(String userName, String password, boolean rememberMe) {
        Result rst = new Result();
        User user = userService.findByName(userName);
        if (user != null) {
            try {
                Subject subject = SecurityUtils.getSubject();
                UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
                token.setRememberMe(rememberMe);
                subject.login(token);//如果返回Null，登录失败

                Session session = subject.getSession();

                session.setAttribute("userId", user.getUserId());
                rst.setFlag(1);
                rst.setContent(user);
            } catch (Exception ex) {
                ex.printStackTrace();
                rst.setFlag(-2);
                rst.setContent("failed");
            }
        } else {
            rst.setFlag(-2);
            rst.setContent("failed");
        }

        return rst;
    }

    @RequestMapping(value = "/logout")
    @ResponseBody
    public Result logout(){
        Result rst = new Result();

        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        rst.setFlag(1);
        rst.setContent("成功退出！");
        return rst;
    }

    @RequestMapping(value = "/register")
    @ResponseBody
    public Result register(User user) {
        Result rst = new Result();
        String password = user.getPassword();
        String salt = new SecureRandomNumberGenerator().nextBytes().toHex();
        user.setSalt(salt);

        SimpleHash simpleHash = new SimpleHash("md5", user.getPassword(), salt + "huhuxs", 2);
        user.setPassword(simpleHash.toString());
        int flag = userService.register(user);

        if (flag == 1) {
//            login(user.getUserName(), password);
            rst.setFlag(flag);
            rst.setContent("success");
        } else {
            rst.setFlag(-2);
            rst.setContent("failed");
        }
        return rst;
    }



}
